We will work with the data provided by the Maddison Project and create a graph showing the GDP per capita growth of selected countries.
We obtain the data from the Maddison Project Database 2020. Then, we import it.
mpd2020 <- as.data.frame(read_excel("mpd2020.xlsx", sheet = "Full data"))
The following command might be useful for country selection. In this case, we’ll compare Chile’s growth with Norway, Sweden, Finland, New Zealand, and Australia.
With the following command, we are interested in seeing the period that will be covered in the graph, considering the available data for Chile.
countrycode country year gdppc
Length:210 Length:210 Min. :1800 Min. : 781
Class :character Class :character 1st Qu.:1861 1st Qu.: 1578
Mode :character Mode :character Median :1914 Median : 3878
Mean :1913 Mean : 5548
3rd Qu.:1966 3rd Qu.: 7292
Max. :2018 Max. :22105
pop
Min. : 771
1st Qu.: 1930
Median : 3678
Mean : 5925
3rd Qu.: 9114
Max. :18676
NA's :11
We see that there is data for Chile from 1800, so the graph will start from that year.
Given the information from the previous two steps, we filter the database to leave it with what we need. We can create a new one in case we need the complete database for something else.
my_palette = c("#DA0000", "#239f40", "#C4961A", "#CC79A7", "#C3D7A4", "#293352")
g1 <- ggplot(data = df_graf1, aes(year, gdppc, color=country)) +
geom_line(size = 1) +
labs(title = "International Comparison of Real GDP per capita in 2011$",
subtitle = "1800-2018",
caption = "Source: Personal elaboration with Maddison Project Database 2020",
x = "",
y = "",
col = NULL) +
scale_color_manual(values = my_palette) +
scale_x_continuous(breaks=seq(1800,2020, by = 10), position = "bottom") +
scale_y_continuous(labels = dollar) +
theme_tufte() +
theme(plot.title = element_text(size = rel(1.4)),
plot.subtitle = element_text(size = rel(1.2), margin=margin(0,0,20,0)),
plot.caption = element_text(hjust = 0, face= "italic"),
axis.text.x = element_text(angle = 90, vjust=.5, size = 10),
axis.text.y = element_text(angle = 0, vjust=0, size = 10),
axis.ticks.length.x = unit(.25, "cm"),
axis.ticks.x = element_line(size = .5),
axis.ticks.y = element_blank(),
panel.grid.major.y = element_line(colour = "grey90"),
panel.grid.minor.y = element_line(colour = "grey90"))
With this command, we get an interactive plot.
fig <- ggplotly(g1)
fig
library(readxl)
library(ggplot2)
library(plotly)
library(Cairo)
library(scales)
library(ggthemes)
library(dplyr)
mpd2020 <- as.data.frame(read_excel("mpd2020.xlsx", sheet = "Full data"))
unique(mpd2020[c("countrycode", "country")])
mpd2020 %>%
filter(countrycode == "CHL") %>%
summary()
df_graf1 <- mpd2020 %>%
filter(countrycode %in% c("CHL", "NOR", "SWE", "FIN", "NZL", "AUS"),
year>=1800)
my_palette = c("#DA0000", "#239f40", "#C4961A", "#CC79A7", "#C3D7A4", "#293352")
g1 <- ggplot(data = df_graf1, aes(year, gdppc, color=country)) +
geom_line(size = 1) +
labs(title = "International Comparison of Real GDP per capita in 2011$",
subtitle = "1800-2018",
caption = "Source: Personal elaboration with Maddison Project Database 2020",
x = "",
y = "",
col = NULL) +
scale_color_manual(values = my_palette) +
scale_x_continuous(breaks=seq(1800,2020, by = 10), position = "bottom") +
scale_y_continuous(labels = dollar) +
theme_tufte() +
theme(plot.title = element_text(size = rel(1.4)),
plot.subtitle = element_text(size = rel(1.2), margin=margin(0,0,20,0)),
plot.caption = element_text(hjust = 0, face= "italic"),
axis.text.x = element_text(angle = 90, vjust=.5, size = 10),
axis.text.y = element_text(angle = 0, vjust=0, size = 10),
axis.ticks.length.x = unit(.25, "cm"),
axis.ticks.x = element_line(size = .5),
axis.ticks.y = element_blank(),
panel.grid.major.y = element_line(colour = "grey90"),
panel.grid.minor.y = element_line(colour = "grey90"))
# To save in png format
ggsave(g1, filename = "plot.png", type = 'cairo', dpi = 300,
width = 18, height = 13.5, units = "cm")
# For higher quality (pdf or eps)
ggsave(g1, filename = "plot.pdf", device = cairo_pdf, dpi = 300,
width = 18, height = 13.5, units = "cm")
ggsave(g1, filename = "plot.eps", device = cairo_ps, dpi = 300,
width = 18, height = 13.5, units = "cm")
knitr::include_graphics("plot.png")
fig <- ggplotly(g1)
fig